package com.surfacing.rcp.client.view;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import com.surfacing.rcp.client.editors.CreateWindow;
import com.surfacing.rcp.client.editors.DienststelleView;
import com.surfacing.rcp.client.editors.EinsatzartView;
import com.surfacing.rcp.client.editors.TransportzielView;
import com.surfacing.rcp.client.editors.UserView;
import com.surfacing.rcp.client.util.ViewHelper;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class SampleView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "com.surfacing.rcp.ui.views.SampleView";
	private Action action1;
	private Action action2;
	private Viewer viewer;

	/*
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */

	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			return new String[] { "One", "Two", "Three" };
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages()
					.getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	class NameSorter extends ViewerSorter {
	}

	/**
	 * The constructor.
	 */
	public SampleView() {
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(2, false));

		Label lblNotarztszstemBruckkapfenberg = new Label(parent, SWT.NONE);
		lblNotarztszstemBruckkapfenberg.setLayoutData(new GridData(SWT.LEFT,
				SWT.CENTER, false, false, 2, 1));
		// lblNotarztszstemBruckkapfenberg.setFont(SWTResourceManager.getFont(
		// "Sans", 16, SWT.BOLD));
		lblNotarztszstemBruckkapfenberg.setAlignment(SWT.CENTER);
		lblNotarztszstemBruckkapfenberg
				.setText("Notarztszstem Bruck/Kapfenberg - Journalblatt");

		Label lblEinenNeuenEinsatz = new Label(parent, SWT.NONE);
		// lblEinenNeuenEinsatz.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		lblEinenNeuenEinsatz.setText("Einen neuen Einsatz anlegen");

		Button btnNeuerEinsatz = new Button(parent, SWT.NONE);
		// btnNeuerEinsatz.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		btnNeuerEinsatz.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		btnNeuerEinsatz.setText("Neuer Einsatz");
		btnNeuerEinsatz.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				ViewHelper.openView(CreateWindow.ID);
			}
		});

		Label lblDasArchiveDurchsehen = new Label(parent, SWT.NONE);
		// lblDasArchiveDurchsehen.setFont(SWTResourceManager.getFont("Sans",
		// 12,
		// SWT.NORMAL));
		lblDasArchiveDurchsehen.setText("Das Archive durchsehen");

		Button btnArchive = new Button(parent, SWT.NONE);
		// btnArchive.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		btnArchive.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		btnArchive.setText("Archive");
		btnArchive.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				ViewHelper.openView(ViewArchive.ID);
			}
		});

		Label lblStatistikErstellen = new Label(parent, SWT.NONE);
		// lblStatistikErstellen.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		lblStatistikErstellen.setText("Statistik erstellen");

		Button btnStatistik = new Button(parent, SWT.NONE);
		// btnStatistik
		// .setFont(SWTResourceManager.getFont("Sans", 12, SWT.NORMAL));
		btnStatistik.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		btnStatistik.setText("Statistik");

		Label lblMitarbeiterPflegen = new Label(parent, SWT.NONE);
		// lblMitarbeiterPflegen.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		lblMitarbeiterPflegen.setText("Mitarbeiter pflegen");

		Button btnMitarbeiter = new Button(parent, SWT.NONE);
		// btnMitarbeiter.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		btnMitarbeiter.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		btnMitarbeiter.setText("Mitarbeiter");
		btnMitarbeiter.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				ViewHelper.openView(UserView.ID);
			}
		});

		Label lblTransportzielePflegen = new Label(parent, SWT.NONE);
		// lblTransportzielePflegen.setFont(SWTResourceManager.getFont("Sans",
		// 12,
		// SWT.NORMAL));
		lblTransportzielePflegen.setText("Transportziele pflegen");

		Button btnTransportziele = new Button(parent, SWT.NONE);
		// btnTransportziele.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		btnTransportziele.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				true, false, 1, 1));
		btnTransportziele.setText("Transportziele");
		btnTransportziele.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				ViewHelper.openView(TransportzielView.ID);
			}
		});

		Label lblDienststellenPflegen = new Label(parent, SWT.NONE);
		// lblDienststellenPflegen.setFont(SWTResourceManager.getFont("Sans",
		// 12,
		// SWT.NORMAL));
		lblDienststellenPflegen.setText("Dienststellen pflegen");

		Button btnDienststellen = new Button(parent, SWT.NONE);
		// btnDienststellen.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		btnDienststellen.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				true, false, 1, 1));
		btnDienststellen.setText("Dienststellen");
		btnDienststellen.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				ViewHelper.openView(DienststelleView.ID);
			}
		});

		Label lblEinsatzartenPflegen = new Label(parent, SWT.NONE);
		// lblEinsatzartenPflegen.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		lblEinsatzartenPflegen.setText("Einsatzarten pflegen");

		Button btnEinsatzarten = new Button(parent, SWT.NONE);
		// btnEinsatzarten.setFont(SWTResourceManager.getFont("Sans", 12,
		// SWT.NORMAL));
		btnEinsatzarten.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		btnEinsatzarten.setText("Einsatzarten");
		btnEinsatzarten.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				ViewHelper.openView(EinsatzartView.ID);
			}
		});

		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				SampleView.this.fillContextMenu(manager);
			}
		});
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(action1);
		manager.add(new Separator());
		manager.add(action2);
	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(action1);
		manager.add(action2);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(action1);
		manager.add(action2);
	}

	private void makeActions() {
		action1 = new Action() {
			public void run() {
				showMessage("Action 1 executed");
			}
		};
		action1.setText("Action 1");
		action1.setToolTipText("Action 1 tooltip");
		action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));

		action2 = new Action() {
			public void run() {
				showMessage("Action 2 executed");
			}
		};
		action2.setText("Action 2");
		action2.setToolTipText("Action 2 tooltip");
		action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
	}

	private void hookDoubleClickAction() {
	}

	private void showMessage(String message) {
		MessageDialog.openInformation(viewer.getControl().getShell(),
				"Sample View", message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		// viewer.getControl().setFocus();
	}
}